package com.example.konwRice;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.example.konwRice.Dao.HistoryDao;
import com.example.konwRice.adapter.item_adapter;
import com.example.konwRice.adapter.search_adapter;
import com.example.konwRice.domain.item;
import com.google.android.material.snackbar.Snackbar;


import java.util.ArrayList;



public class search extends Fragment {
    private View view;
    private SearchView searchView;
    private ListView listView;
    private HistoryDao historyDao;
    private ArrayList<item> newsItems=new ArrayList<>();



    public search() {
        // Required empty public constructor
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_search, container, false);
        searchView = (SearchView) root.findViewById(R.id.searchView);
        listView = (ListView) root.findViewById(R.id.lv_load_search);
        searchView.setSubmitButtonEnabled(true);
        // 设置搜索文本监听
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                //伪搜索
                root.setVisibility(View.VISIBLE);
                historyDao = new HistoryDao(root.getContext());
                newsItems = historyDao.Search(query);
                search_adapter adapter = new search_adapter(root.getContext(), R.layout.new_item, newsItems);


                listView.setAdapter(adapter);
                //清除焦点，收软键盘
                //mSearchView.clearFocus();

                return false;
            }

            // 当搜索内容改变时触发该方法

            @Override
            public boolean onQueryTextChange(String newText) {
                //do something
                //当没有输入任何内容的时候清除结果，看实际需求
                if (TextUtils.isEmpty(newText)) root.setVisibility(View.VISIBLE);
                return false;
            }


        });


        return root;
    }
}